<template>
	<!-- 为整个页面添加一个判断 让数据出来之后再显示，就不会出现花括号或者闪烁的问题 -->
	<view class="goods_detail-page" v-if="goodsInfo.goods_name">
		<!-- 顶部轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, index) in goodsInfo.pics" :key="index">
				<image :src="item.pics_mid" @click="preview(index)"></image>
			</swiper-item>
		</swiper>
		
		<!-- 文字区域 -->
		<view class="goods-detail-info">
			<view class="goods-detail-price">￥{{goodsInfo.goods_price}}</view>
			<view class="goods-detail-title">
				<view class="goods-detail-name">{{goodsInfo.goods_name}}</view>
				<view class="goods-detail-star">
					<uni-icons type="star" size="13"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="goods-detail-deliver">快递：免运费</view>
		</view>
		<!--详情区域-->
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		
		<!--底部导航区域-->
		<view class="goods-detail-bottom">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
		
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from 'vuex'
	export default {
		computed:{
			// 按需加载，调用mapSate方法把m_cart模块中的cart数组映射到当前页面中做我计算属性来使用
			// ...mapState('模块名称',['要映射的数据名称1','要赢色的数据名称2'])
			...mapState('m_cart',['cart','a']),
			...mapGetters('m_cart', ['cartNum']),
		},
		watch:{
			cartNum:{
				handler(newVal){
					const result = this.options.find(x=>x.icon=='cart')
					if(result){
						result.info=newVal
					}
				},
				immediate: true
			}
		},
		data() {
			return {
				goodsId: '',
				goodsInfo :{},
				options: [{
							icon: 'shop',
							text: '店铺'
						}, {
							icon: 'cart',
							text: '购物车',
							info: ''
						}],
				buttonGroup: [{
					      text: '加入购物车',
					      backgroundColor: '#ff0000',
					      color: '#fff'
					    },
					    {
					      text: '立即购买',
					      backgroundColor: '#ffa200',
					      color: '#fff'
					    }],
			};
		},
		onLoad(options){
			this.goodsId = options.goods_id || '';
			// console.log(options,this.goodsId)
			this.getgoodsDetail();
		},
		methods:{
			// 把m_cart模块中的addTocart方法映射到当前页面中
			...mapMutations('m_cart',['addToCart']),
			
			async getgoodsDetail(){
				const {data: res} = await uni.$http.get(uni.api.DETAIL,{goods_id:this.goodsId});
				if(res.meta.status!==200)return uni.$showMsg();
				// console.log('goodsDetail',res)
				// 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
				  res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
				  // 由于ios系统对webp格式的图片支持不够可能导致显示不出来的问题，可以将其换成jpg格式
				  // res.message.goods_introduce = res.message.goods_introduce.replace(/webp/g, 'jpg')
				this.goodsInfo = res.message
			},
			// 轮播图大图预览
			preview(num){
				uni.previewImage({
					current: num,
					urls:this.goodsInfo.pics.map(x=>x.pics_big),
					// indicator: "default", 微信小程序暂不支持
					// loop: true,
					/* longPressActions:{
						itemList:['发送给朋友','保存图片','收藏'],
						itemColor: #c00000,
						success(data){
							console.log('成功2',data)
						},
						fail(data){
							console.log('失败2',data)
						},
						complete(data){
							console.log('完成2',data)
						}
					}, */
					 success(data){
						console.log('成功',data)
					},
					fail(data){
						console.log('失败',data)
					},
					complete(data){
						console.log('完成',data)
					} 
				})
			},
			onClick(e){
				// console.log('e1', e)
				if(e.index == 0){
					//店铺
				}else{
					// 购物车
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			buttonClick(e){
				// console.log('e2', e)
				if(e.index == 0){
					// 加入购物车
					const goods={
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: true
					}
					this.addToCart(goods)
				}else{
					// 立即购买
				}
				
			}
		}
	}
</script>

<style lang="scss">
	page{
		background:#ffffff;
	}
	.goods_detail-page{
		position: relative;
		height: 100%;
		box-sizing: border-box;
		padding-bottom: 50px;
	}
swiper{
	height: 750rpx;
	image{
		height: 100%;
		width: 100%;
	}
}

.goods-detail-info{
	padding-left: 20rpx;
	margin-top: 20rpx;
	.goods-detail-price{
		color: #c00000;
		font-size: 36rpx;
		margin: 10rpx 0;
	}
	.goods-detail-title{
		display: flex;
		justify-content: space-between;
		.goods-detail-name{
			font-size: 26rpx;
		}
		.goods-detail-star{
			display: flex;
			flex-direction: column;
			align-items: center;
			color: gray;
			font-size: 24rpx;
			border-left: 1px solid #efefef;
			width: 260rpx;
		}
	}
	.goods-detail-deliver{
		color: gray;
		font-size: 24rpx;
		margin: 30rpx 0;
	}
}
.goods-detail-bottom{
	width: 100%;
	position: fixed;
	bottom: 0;
	left: 0;
	// z-index: 999;
}
</style>
